package com.snap.awesomeserial.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snap.awesomeserial.R;
import com.snap.awesomeserial.bean.NAStep;
import com.snap.awesomeserial.dialog.NumberInputDialog;
import com.snap.awesomeserial.ui.DemoEditActivity;
import com.snap.awesomeserial.ui.widget.AlphaLinearLayout;

import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StepEditAdapter extends RecyclerView.Adapter<StepEditAdapter.Holder> {
    private Context context;
    private List<NAStep> steps;

    public StepEditAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.step_edit_item, parent, false);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        if (steps == null || steps.size() == 0 || position == steps.size()) {
            holder.stepAddLl.setVisibility(View.VISIBLE);
            holder.stepItem.setVisibility(View.GONE);
            holder.checkBox.setVisibility(View.GONE);
            holder.stepAddLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    steps.add(new NAStep(1, "", 1, 1, 800, 3, 20, -1));
                    notifyDataSetChanged();
                }
            });
            return;
        }

        holder.stepAddLl.setVisibility(View.GONE);
        holder.stepItem.setVisibility(View.VISIBLE);
        holder.checkBox.setVisibility(View.VISIBLE);
        NAStep step = steps.get(position);
        holder.checkBox.setChecked(step.isChecked());
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                step.setChecked(isChecked);
            }
        });
        holder.nameTv.setText(step.getName());
        nameFocusSetting(holder.nameTv,step);
        indexFocusSetting(holder.indexTv, "", step.getIndex(), 1, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setIndex((int) value);
            }
        });
        focusSetting(holder.magnetTimeTv, "s", step.getMagnetTime(), 550, 3, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setMagnetTime((int) value);
            }
        });
        focusSetting(holder.mixtureTimeTv, "min", step.getMixtureTime(), 376, 2, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setMixtureTime((int) value);
            }
        });
        focusSetting(holder.mixtureSpeedTv, "", step.getMixtureSpeed(), 464, 1, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setMixtureSpeed((int) value);
            }
        });
        focusSetting(holder.temperatureTv, "°C", step.getTemperature(), 352, 5, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setTemperature(value);
            }
        });
        focusSetting(holder.volumeTv, "ul", step.getVolume(), 256, 4, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setVolume((int) value);
            }
        });
        focusSetting(holder.waitTimeTv, "min", step.getWaitTime(), 280, 2, new NumberInputDialog.OnValueListener() {
            @Override
            public void onValueChanged(float value) {
                step.setWaitTime((int) value);
            }
        });
    }

    private void nameFocusSetting(EditText e, NAStep step) {
        e.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    e.setBackground(context.getDrawable(R.drawable.bg_step_item_focus));

                } else {
                    e.setBackground(context.getDrawable(R.drawable.bg_step_item));
                    step.setName(e.getText().toString());
                }
            }
        });
    }

    private void indexFocusSetting(TextView textView, String unit, float value, int maxLength, NumberInputDialog.OnValueListener listener) {
        textView.setText(value == -1 ? "-" : String.format(Locale.CHINA, "%s", new BigDecimal(String.valueOf(value)).stripTrailingZeros().toPlainString()));
        FragmentManager fragmentManager = ((DemoEditActivity) context).getSupportFragmentManager();
        NumberInputDialog dialog = new NumberInputDialog().setParam(textView, unit, 96, maxLength, listener);
        textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    textView.setBackground(context.getDrawable(R.drawable.bg_step_item_focus));
                    dialog.show(fragmentManager, "");
                } else {
                    textView.setBackground(context.getDrawable(R.drawable.bg_step_item));
                }
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show(fragmentManager, "");
            }
        });
    }

    public void focusSetting(TextView textView, String unit, float value, int xDp, int maxLength, NumberInputDialog.OnValueListener listener) {
        textView.setText(value == -1 ? "-" : String.format(Locale.CHINA, "%s%s", new BigDecimal(String.valueOf(value)).stripTrailingZeros().toPlainString(), unit));
        FragmentManager fragmentManager = ((DemoEditActivity) context).getSupportFragmentManager();
        NumberInputDialog dialog = new NumberInputDialog().setParam(textView, unit, xDp, maxLength, listener);
        textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    textView.setBackground(context.getDrawable(R.drawable.bg_step_item_focus));
                    dialog.show(fragmentManager, "");
                } else {
                    textView.setBackground(context.getDrawable(R.drawable.bg_step_item));

                }
            }
        });
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show(fragmentManager, "");
            }
        });
    }

    @Override
    public int getItemCount() {
        return (steps == null || steps.size() == 0) ? 1 : steps.size() + 1;
    }

    public void bindDataList(List<NAStep> steps) {
        this.steps = steps;
        notifyDataSetChanged();
    }


    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.index_tv)
        TextView indexTv;
        @BindView(R.id.name_tv)
        EditText nameTv;
        @BindView(R.id.wait_time_tv)
        TextView waitTimeTv;
        @BindView(R.id.mixture_time_tv)
        TextView mixtureTimeTv;
        @BindView(R.id.mixture_speed_tv)
        TextView mixtureSpeedTv;
        @BindView(R.id.magnet_time_tv)
        TextView magnetTimeTv;
        @BindView(R.id.volume_tv)
        TextView volumeTv;
        @BindView(R.id.temperature_tv)
        TextView temperatureTv;
        @BindView(R.id.step_add_ll)
        AlphaLinearLayout stepAddLl;
        @BindView(R.id.step_item)
        LinearLayout stepItem;
        @BindView(R.id.checkbox)
        CheckBox checkBox;

        Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
